package com.hbxy.favorite.service.impl;
import com.hbxy.favorite.dao.CollectMapper;
import com.hbxy.favorite.dao.CommentMapper;
import com.hbxy.favorite.dao.FavoritesMapper;
import com.hbxy.favorite.dao.UserMapper;
import com.hbxy.favorite.domain.*;
import com.hbxy.favorite.resault.Response;
import com.hbxy.favorite.resault.ResponseBo;
import com.hbxy.favorite.service.CollectService;
import com.hbxy.favorite.utils.RegUtil;
import com.hbxy.favorite.vo.CollectVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class CollectServiceImpl implements CollectService {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    FavoritesMapper favoritesMapper;
    @Autowired
    CommentMapper commentMapper;
    @Override
    public ResponseBo addCollect(Collect collect) {

        collect.setCreateTime(new Date().getTime());
        collect.setLastModifyTime(new Date().getTime());
        collect.setIsDelete("0");

        if(RegUtil.isNotNull(collect.getFavoritesId())){

            collectMapper.insert(collect);

            return ResponseBo.ok();
        }
        else{
            collect.setFavoritesId(1L);//未读列表
            collectMapper.insert(collect);
            return ResponseBo.ok();
        }
    }

    @Override
    public List<CollectVo> getAllCollect(String title) {

        List<Collect> collectList;
        List<CollectVo> collectVoList = new ArrayList<>();
        if(title != null){
            CollectExample example = new CollectExample();
            CollectExample.Criteria criteria = example.createCriteria();
            criteria.andTitleLike("%"+title+"%");
            collectList = collectMapper.selectByExample(example);
            log.info("查询结果："+collectList);
        }else {
            CollectExample example1 = new CollectExample();
            collectList = collectMapper.selectByExample(example1);
            }
        for (Collect collect:collectList) {
            if(!"1".equals(collect.getIsDelete())&& !"1".equals(collect.getType())){
                CollectVo collectVo =new CollectVo();

                CommentExample commentExample =new CommentExample();
                CommentExample.Criteria criteria1 = commentExample.createCriteria();
                criteria1.andCollectIdEqualTo(collect.getId());

                List<Comment> commentList =commentMapper.selectByExample(commentExample);

                if(RegUtil.isNotNull(collect.getFavoritesId()))
                {
                    Favorites favorites =favoritesMapper.selectByPrimaryKey(collect.getFavoritesId());
                    collectVo.setFavoriteName(favorites.getName());
                }
                User user=userMapper.selectByPrimaryKey(collect.getUserId());
                collectVo.setId(collect.getId());
                collectVo.setTitle(collect.getTitle());
                collectVo.setCategory(collect.getCategory());
                collectVo.setUserId(collect.getUserId());
                collectVo.setUrl(collect.getUrl());
                collectVo.setUserName(user.getUserName());
                collectVo.setLogoUrl(collect.getLogoUrl());
                collectVo.setCommentList(commentList);
                collectVo.setCommentSize(commentList.size());
                collectVoList.add(collectVo);

            }
        }
        return collectVoList;

    }
    @Override
    public ResponseBo deleteCollect(Collect collect) {

        if(RegUtil.isNotNull(collect.getId()))
        {   Collect collect1 =collectMapper.selectByPrimaryKey(collect.getId());
            collect1.setIsDelete("1");
            collectMapper.updateByPrimaryKey(collect1);
            return ResponseBo.ok();
        }
        return ResponseBo.error();
    }

    @Override
    public List<CollectVo> getCollectByFavoriteId(Long id) {

        List<Collect> collectList;
        List<CollectVo> collectVoList =new ArrayList<>();

            CollectExample example = new CollectExample();
            CollectExample.Criteria  criteria= example.createCriteria();
            criteria.andFavoritesIdEqualTo(id);
            collectList = collectMapper.selectByExample(example);

            for(Collect collect:collectList){
                if(!"1".equals(collect.getIsDelete())){

                    CollectVo collectVo =new CollectVo();
                    Favorites favorites =favoritesMapper.selectByPrimaryKey(collect.getFavoritesId());
                    CommentExample commentExample =new CommentExample();
                    CommentExample.Criteria criteria1 = commentExample.createCriteria();
                    criteria1.andCollectIdEqualTo(collect.getId());
                    List<Comment> commentList =commentMapper.selectByExample(commentExample);
                    User user=userMapper.selectByPrimaryKey(collect.getUserId());

                    collectVo.setFavoriteName(favorites.getName());
                    collectVo.setTitle(collect.getTitle());
                    collectVo.setId(collect.getId());
                    collectVo.setUrl(collect.getUrl());
                    collectVo.setUserId(collect.getUserId());
                    collectVo.setCategory(collect.getCategory());
                    collectVo.setUserName(user.getUserName());
                    collectVo.setLogoUrl(collect.getLogoUrl());
                    collectVo.setCommentList(commentList);
                    collectVo.setCommentSize(commentList.size());
                    collectVoList.add(collectVo);
                }
            }
            return collectVoList;
    }

    @Override
    public List<CollectVo> getNoRead(Long userId) {

        List<CollectVo> collectVoList =new ArrayList<>();

        if(RegUtil.isNotNull(userId)){

            CollectExample example = new CollectExample();
            CollectExample.Criteria criteria =example.createCriteria();
            criteria.andUserIdEqualTo(userId);
            List<Collect> collectList = collectMapper.selectByExample(example);
            for(Collect c:collectList){
                if(!"1".equals(c.getIsDelete()) && c.getFavoritesId()==1L){
                    CollectVo collectVo =new CollectVo();
                    collectVo.setId(c.getId());
                    User user=userMapper.selectByPrimaryKey(c.getUserId());
                    Favorites favorites =favoritesMapper.selectByPrimaryKey(c.getFavoritesId());
                    CommentExample commentExample =new CommentExample();
                    CommentExample.Criteria criteria1 = commentExample.createCriteria();
                    criteria1.andCollectIdEqualTo(c.getId());
                    List<Comment> commentList =commentMapper.selectByExample(commentExample);
                    collectVo.setTitle(c.getTitle());
                    collectVo.setFavoriteName(favorites.getName());
                    collectVo.setCategory(c.getCategory());
                    collectVo.setUrl(c.getUrl());
                    collectVo.setUserId(c.getUserId());
                    collectVo.setUserName(user.getUserName());
                    collectVo.setLogoUrl(c.getLogoUrl());
                    collectVo.setCommentSize(commentList.size());
                    collectVo.setCommentList(commentList);
                    collectVoList.add(collectVo);
                }
            }
        }
        log.info("未读列表："+collectVoList);
        return collectVoList;
    }

    @Override
    public List<CollectVo> getCollectByUserId(Long userId) {

        List<Collect> collectList;
        List<CollectVo> collectVoList =new ArrayList<>();

        CollectExample example = new CollectExample();
        CollectExample.Criteria  criteria= example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        collectList = collectMapper.selectByExample(example);


        for(Collect collect:collectList){
            if(!"1".equals(collect.getIsDelete())){

                CollectVo collectVo =new CollectVo();
                Favorites favorites =favoritesMapper.selectByPrimaryKey(collect.getFavoritesId());
                CommentExample commentExample =new CommentExample();
                CommentExample.Criteria criteria1 = commentExample.createCriteria();
                criteria1.andCollectIdEqualTo(collect.getId());

                List<Comment> commentList =commentMapper.selectByExample(commentExample);
                User user=userMapper.selectByPrimaryKey(collect.getUserId());

                collectVo.setTitle(collect.getTitle());
                collectVo.setFavoriteName(favorites.getName());

                collectVo.setUrl(collect.getUrl());
                collectVo.setId(collect.getId());

                collectVo.setUserId(collect.getUserId());
                collectVo.setCategory(collect.getCategory());

                collectVo.setLogoUrl(collect.getLogoUrl());
                collectVo.setUserName(user.getUserName());
                collectVo.setCommentList(commentList);
                collectVo.setCommentSize(commentList.size());
                collectVoList.add(collectVo);
            }
        }
        return collectVoList;


    }

    @Override
    public List<CollectVo> getDeleted(Long userId) {

        List<CollectVo> collectVoList = new ArrayList<>();
        int size =0;
        CollectExample example =new CollectExample();
        CollectExample.Criteria criteria =example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<Collect> collectList = collectMapper.selectByExample(example);
        for (Collect collect:collectList) {
            if(!"0".equals(collect.getIsDelete())){
                CollectVo Vo =new CollectVo();

                CommentExample example1 =new CommentExample();
                CommentExample.Criteria criteria1 = example1.createCriteria();
                criteria1.andCollectIdEqualTo(collect.getId());
                List<Comment> commentList =commentMapper.selectByExample(example1);
                size=commentList.size();
                User user=userMapper.selectByPrimaryKey(collect.getUserId());
                Vo.setId(collect.getId());
                Vo.setTitle(collect.getTitle());
                Vo.setCategory(collect.getCategory());
                Vo.setUrl(collect.getUrl());
                Vo.setUserName(user.getUserName());
                Vo.setLogoUrl(collect.getLogoUrl());
                Vo.setCommentList(commentList);
                Vo.setCommentSize(size);
                collectVoList.add(Vo);

            }
        }
        return collectVoList;
    }

    @Override
    public int deletedForever(Collect collect) {

      int flag =  collectMapper.deleteByPrimaryKey(collect.getId());

        return flag;
    }


}
